package com.itzdm.controller;

import com.alibaba.fastjson.JSONObject;
import com.itzdm.anno.Log;
import com.itzdm.pojo.*;
import com.itzdm.service.CarService;
import com.itzdm.service.UserService;
import com.itzdm.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CarService carService;

    @Log
    @PostMapping("/selectVehicle")
    public Result selectVehicle(@RequestBody Page page) {
        log.info("分页查询：page:{}", page);
        PageBean pageBean = carService.page(page);
        return Result.success(pageBean,"查询成功");
    }

    @Log
    @RequestMapping("/rentVehicle")
    public Result rent(@RequestHeader("token") String token,@RequestBody JSONObject jsonObject) {
        log.info("租车：{}", jsonObject);
        return userService.rent(token,jsonObject);
    }

    @Log
    @PostMapping("/returnVehicle")
    public Result backCar(@RequestBody Car car){
        log.info("还车:{}",car);
        userService.backCar(car);
        return Result.success(null,"还车成功");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        log.info("用户：{}",user);
        Result result = userService.login(user);
        return result;
    }

    @Log
    @RequestMapping("/charge")
    public Result charge(@RequestHeader("token") String token, @RequestBody JSONObject jsonObject){
        return userService.charge(token,jsonObject);
    }

    @Log
    @RequestMapping("/rentingVehicle")
    public Result rentingVehicle(){
        return Result.success("成功");
    }
    @RequestMapping("/register")
    public Result register(String username,String pwd,String name,String idNumber){
        return userService.register(username,pwd,name,idNumber);
    }

    @Log
    @RequestMapping("/getInfo")
    public Result getInfo(@RequestHeader("token") String token){
        double remainMoney = userService.getInfo(token);
        return Result.success(remainMoney, "查询成功");
    }
}
